package com.tien.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;

import com.tien.model.User;
import com.tien.service.UserService;
import com.tien.utils.AbstractAction;
import com.tien.utils.CallBackJSONUtil;
import com.tien.utils.SecurityUserUtil;

/**
 * 
 * @author Tian
 * @Mail tianxin_star@163.com
 * @version date：Mar 13, 2012 1:59:46 PM
 * 
 */
@Controller
@Scope("prototype")
public class UserManagerAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5628523530525547088L;
	private static final Log logger = LogFactory
			.getLog(UserManagerAction.class);
	@Autowired
	private UserService userService;
	private User user;
	private List<User> users;
	
	public String execute() {
		try {
			user = SecurityUserUtil.getCurrentUser();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	public String findAllUsers(){
		try{
			users = userService.findAllUsers(Integer.parseInt(this.getStart()), Integer.parseInt(this.getLimit()));
			Long totalRows = userService.findAllUsersTotalRows();
			this.doSetPagingBean(Integer.parseInt(totalRows.toString()));
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return "users";
	}
	
	public String updateUser(){
		try {
			CallBackJSONUtil.printSuccessState(null, response);
		} catch (AccessDeniedException e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printAccessDeniedState(e, response);
		} catch (Exception e) {
			logger.error(e.getMessage());
			CallBackJSONUtil.printErrorState(e, response);
		}
		return null;
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

}
